package com.bbx.taxi.client.DB;

import com.bbx.api.gclient.GMsg;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

/**
 *离线消息
 *
 */
public class JmsgDB  implements DBcolumns{
	private DBHelper helper;
	public JmsgDB(Context context) {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(context,myApplication.getUid());
	}
	/**
	 *  打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() { 
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 *  打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() { 
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}
	/**
	 *查询全部
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_MESSAGE, null, null, null, null,
				null, MESSAGE_ID + " desc");
		return cursor;

	}
//	/**
//	 *用于分页查询，指定数量，指定id,按降序查询，不能大于id,类型不能等于GMsg.ONLINE_RELOGIN，登陆踢下线，和订单完成，新的审核订单,价格更新
//	 */
//	public Cursor onQuery(int id,int count) {
//		String tiaojiao=MESSAGE_TYPE+"<>"+GMsg.ONLINE_RELOGIN+" and "+MESSAGE_TYPE+"<>"+GMsg.ONLINE_FINISH+" and "+MESSAGE_TYPE+"<>"+GMsg.ONLINE_ONTLINE_PAY_SUCC
//				+" and "+MESSAGE_TYPE+"<>"+GMsg.ONLINE_NEW_UNPROCESSED
//		        +" and "+MESSAGE_TYPE+"<>"+GMsg.OFFICAIL_ONLINE_PRICE;
////				+" and "+MESSAGE_TYPE+"<>"+GMsg.ONLINE_OUTLINE_PAY_SUCC;
//		SQLiteDatabase SQLdb=onOpenRead();
//		Cursor cursor = null;
//		if(id==-1){
//			 cursor = SQLdb.query(TABLE_MESSAGE, null,tiaojiao, null, null,
//						null, MESSAGE_ID + " desc LIMIT 0,"+count);
//		}
//		else{
//			 cursor = SQLdb.query(TABLE_MESSAGE, null,MESSAGE_ID+"<"+id+" and "+tiaojiao, null, null,
//					null, MESSAGE_ID + " desc LIMIT "+count);
//		}
//		
//		return cursor;
//
//	}
	/**
	 *用于分页查询，指定数量，指定id,按降序查询，用等于
	 */
	public Cursor onQuery(int id,int count) {
		String tiaojiao=MESSAGE_TYPE+"="+GMsg.OFFICAIL_ONLINE_PAIDAN
				+" or "+MESSAGE_TYPE+"="+GMsg.OFFICAIL_ONLINE_CANCEL
				+" or "+MESSAGE_TYPE+"="+GMsg.OFFICAIL_ONLINE_GAIPAI
				+" or "+MESSAGE_TYPE+"="+GMsg.OFFICAIL_ONLINE_ONCAR
		        +" or "+MESSAGE_TYPE+"="+GMsg.OFFICAIL_ONLINE_OFFCAR
		        +" or "+MESSAGE_TYPE+"="+GMsg.OFFICAIL_ONLINE_ROUND_TRIP
		        +" or "+MESSAGE_TYPE+"="+GMsg.ONLINE_XIADAN
		        +" or "+MESSAGE_TYPE+"="+GMsg.OFFICAIL_AA;
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = null;
		if(id==-1){
			 cursor = SQLdb.query(TABLE_MESSAGE, null,tiaojiao, null, null,
						null, MESSAGE_ID + " desc LIMIT 0,"+count);
		}
		else{
			 cursor = SQLdb.query(TABLE_MESSAGE, null,MESSAGE_ID+"<"+id+" and ("+tiaojiao+")", null, null,
					null, MESSAGE_ID + " desc LIMIT "+count);
		}
		
		return cursor;

	}
	/**
	 *根据订单ID，查询相关信息，按正序排列下。
	 */
	public Cursor onQuery(String order_id) {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_MESSAGE, null, MESSAGE_ORDER_ID+"='"+order_id+"'", null, null,
				null, MESSAGE_ID + " ");
		return cursor;

	}
	/**
	 * 删除整个表
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_MESSAGE, null, null);
		SQLdb.close();
	}

	/**
	 * 删除
	 */
	public void onDelete(String rowId) {
		SQLiteDatabase SQLdb=onOpenWrite();
		 String[] whereArgs = { String.valueOf(rowId) };
		 int i =SQLdb.delete(TABLE_MESSAGE, MESSAGE_ID + "=?", whereArgs);
		 SQLdb.close();
	}
	/**
	 *更新一条数据
	 */
	public void onUpdate(int id,String order_id,String time,int type,String json) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MESSAGE_ID, id);
		cv.put(MESSAGE_ORDER_ID,order_id);
		cv.put(MESSAGE_TIME, time);
		cv.put(MESSAGE_TYPE, type);
		cv.put(MESSAGE_JSON, json);
		SQLdb.update(TABLE_MESSAGE, cv, MESSAGE_ID + "=" + id, null);
		SQLdb.close();
	}
	/**
	 * 更新一条数据,更新是否已读状态
	 */
	public void onUpdate(int id,String isread) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MESSAGE_READ, isread);
		SQLdb.update(TABLE_MESSAGE, cv, MESSAGE_ID + "=" + id, null);
		SQLdb.close();
	}
	/**
	 * 插入一条数据 
	 */
	public void onWrite(final String order_id,final String time,final int type,final String json) {
	 new AsyncTask<Void, Void, Void>() {
		@Override
		protected Void doInBackground(Void... arg0) {

			SQLiteDatabase SQLdb=onOpenWrite();
			ContentValues values = new ContentValues();
			values.put(MESSAGE_ORDER_ID,order_id);
			values.put(MESSAGE_TIME, time);
			values.put(MESSAGE_TYPE, type);
			values.put(MESSAGE_JSON, json);
			//默认未读状态
			values.put(MESSAGE_READ,Value.MSG_UNREAD);
			SQLdb.insert(TABLE_MESSAGE, MESSAGE_ID, values);
			SQLdb.close();
			return null;
		}}.execute();

	}
	/**
	 * 关闭数据库
	 */
	public void onColse(){
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.close();
	}
}
